package com.aerospace.util;

import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * @ClassName ImageUtils
 * @Description TODO
 * @Author Aero
 * @Date 2019/9/18 1:20
 * @Version 1.0
 **/
public class ImageUtils {
    public static String IMAGE_PATH = "/image";

    public static ImageView getImageView(String pic) {
        return getImageView(pic, 32, 32);
    }

    public static ImageView getImageView(String pic, double width, double height) {
        Image image = getImage(pic);
        if (image == null) {
            return null;
        }

        ImageView imageView = new ImageView(image);
        imageView.setFitHeight(width);
        imageView.setFitWidth(height);
        return imageView;
    }

    public static Image getImage(String pic) {
        System.out.println(IMAGE_PATH + "/" + pic);
//        System.out.println(ImageUtils.class.getClassLoader().getResource(""));
//        System.out.println(ImageUtils.class.getClassLoader().getResource("/"));

//        String fileName = ImageUtils.class.getClassLoader().getResource(pic).getPath();
//        System.out.println(fileName);
        InputStream is = ImageUtils.class.getResourceAsStream( IMAGE_PATH + "/" + pic);
//        InputStream is = null;
//        try {
//            is = new BufferedInputStream(
//                    new FileInputStream( IMAGE_PATH + "/" + pic));
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
        return new Image(is);
    }
}
